import store from "@/store"
import { analysisComponentTypes } from "./const"
import { getComponentListByTypes } from "./utils"
import { getAllAnalysisComponentTypes } from "./analysisData"

/**
 * 保存,返回当前的画布和页面配置信息
 * @returns 
 */
export function save (){
  const {
    canvasComponentList,
    pageSetting
  } = store.state.lowCode
  const componentList = canvasComponentList.map(component => {
    const item = {
      ...component,
      style: {
        ...component.style
      },
      props: {
        ...component.props
      }
    }

    // if(item.tag.indexOf('picker') > -1 && item.props.value ){
    //   if(item.props.value instanceof Array){
    //     item.props.value = item.props.value.map(date => date.format(item.props.format))
    //   }
    //   else {
    //     if(item.props.value._d){
    //       const value = item.props.value.format(item.props.format)
    //       item.props.value = value
    //     }
        
    //   }
    // }
    return item
  })
  console.log(componentList)
  // 获取所有业务组件
  const types = getAllAnalysisComponentTypes()

  const itemList = getComponentListByTypes(componentList, types).map(item => {
    const value = item.props.value
    // delete value.params
    return {
      ...value, 
      categoryId: value.componentCategoryId,
      componentPageId: item.key
    }
  })
  const pageOperation = JSON.stringify({componentList, pageSetting})
  return {
    itemList,
    pageOperation
  }
}